#include <gtest/gtest.h>
#include <pingcap/Exception.h>
#include <pingcap/kv/Scanner.h>
#include <pingcap/kv/Snapshot.h>
#include <pingcap/kv/Txn.h>
#include <tiredis/TiKvCluster.h>
#include <tiredis/TLogger.h>

namespace pingcap
{
namespace kv
{

// Just for test purpose
struct TestTwoPhaseCommitter
{
private:
    TwoPhaseCommitter committer;

public:
    TestTwoPhaseCommitter(Txn *txn) : committer{txn} {}

    void prewriteKeys(Backoffer &bo, const std::vector<std::string> &keys) { committer.prewriteKeys(bo, keys); }

    void commitKeys(Backoffer &bo, const std::vector<std::string> &keys) { committer.commitKeys(bo, keys); }

    std::vector<std::string> keys() { return committer.keys; }

    void setCommitTS(int64_t commit_ts) { committer.commit_ts = commit_ts; }
};

}
}

TEST(TikvCliTests, TxnSetGetKey)
{
    LogManager::Instance().Log().information("tiredis");                                            
    pingcap::kv::ClusterPtr test_cluster;
    std::vector<std::string> pd_addrs{"127.0.0.1:2379"};
    test_cluster = createCluster(pd_addrs);
    pingcap::kv::Txn txn(test_cluster.get());
    txn.set("a", "a");
    txn.set("b", "b");
    for(size_t i = 0; i < 100000; i++){
        txn.set(std::to_string(i) , std::to_string(i));
    }
    txn.commit();
    pingcap::kv::Snapshot snap(test_cluster.get());
    std::string a = snap.Get("a");
    std::string abc = snap.Get("abc");
    std::cout << "get a status:" << a.back() << std::endl;
    std::cout << "get abc status:" << abc.back() << std::endl;
    a.pop_back();
    abc.pop_back();
    std::cout << "get a:" << a << std::endl;
    std::cout << "get abc:" << abc << std::endl;
}
